//
//  XNTarBar.m
//  SmartHome
//
//  Created by 赵嘉诚 on 15/3/15.
//  Copyright (c) 2015年 赵嘉诚. All rights reserved.
//

#import "XNTarBar.h"

@interface XNTarBar ()
{
    NSMutableArray *buttonInfoArray;
}

@property (nonatomic, weak) UIButton *selectedBtn;

@end

@implementation XNTarBar

- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (void)addButtonWithImage:(UIImage *)image selectedImage:(UIImage *)selectedImage andTitlt:(NSString *)title
{
    UIButton *btn = [[UIButton alloc] init];
    
    NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:image, @"image", selectedImage, @"selectedImage", title, @"title",nil];
    
    if(!buttonInfoArray){
        buttonInfoArray = [[NSMutableArray alloc] init];
    }
    [buttonInfoArray addObject:dic];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    UILabel *label = [[UILabel alloc] init];
    [imageView setFrame:CGRectMake(0, 0, 35, 35)];
    imageView.center = CGPointMake(40, 20);
    [label setFrame:CGRectMake(0, 0, 80, 20)];
    label.center = CGPointMake(40, 55);
    label.text = title;
    label.textColor = TEXT_GRAY_COLOR;
    label.textAlignment = NSTextAlignmentCenter;
    
    [btn addSubview:imageView];
    [btn addSubview:label];
    
    [self addSubview:btn];
    
    //带参数的监听方法记得加"冒号"
    [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    //如果是第一个按钮, 则选中(按顺序一个个添加)
    if (self.subviews.count == 1) {
        [self clickBtn:btn];
    }
}

- (void)selectedButton:(UIButton *)button
{
    if(!button){
        return ;
    }
    UIImage *image;
    UIImage *selectedImage;
    NSString *title;
    NSDictionary *dic;
    for (int i = 0; i < 4 && i < self.subviews.count; i++) {
        UIButton *b = self.subviews[i];
        if(b == button){
            dic = [buttonInfoArray objectAtIndex:i];
            image = [dic objectForKey:@"image"];
            selectedImage = [dic objectForKey:@"selectedImage"];
            title = [dic objectForKey:@"title"];
        }
    }
    UIView *view;
    for (view in button.subviews) {
        [view removeFromSuperview];
    }
    UIImageView *imageView;
    UILabel *label = [[UILabel alloc] init];
    if(button.selected){
        imageView = [[UIImageView alloc] initWithImage:selectedImage];
        label.textColor = [UIColor whiteColor];
        [button setBackgroundColor:TEXT_GREEN_COLOR];
    }else{
        imageView = [[UIImageView alloc] initWithImage:image];
        label.textColor = TEXT_GRAY_COLOR;
        [button setBackgroundColor:BACKGROUND_COLOR];
    }
    [imageView setFrame:CGRectMake(0, 0, 35, 35)];
    imageView.center = CGPointMake(40, 20);
    [label setFrame:CGRectMake(0, 0, 80, 20)];
    label.center = CGPointMake(40, 55);
    label.text = title;
    label.textAlignment = NSTextAlignmentCenter;
    [button addSubview:imageView];
    [button addSubview:label];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    NSUInteger count = self.subviews.count;
    for (int i = 0; i < count; i++) {
        //取得按钮
        UIButton *btn = self.subviews[i];
        
        btn.tag = i; //设置按钮的标记, 方便来索引当前的按钮,并跳转到相应的视图
        
        CGFloat x = i * self.bounds.size.width / count;
        CGFloat y = 0;
        CGFloat width = self.bounds.size.width / count;
        CGFloat height = self.bounds.size.height;
        btn.frame = CGRectMake(x, y, width, height);
    }  
}

- (void)clickBtn:(UIButton *)button {
    //1.先将之前选中的按钮设置为未选中
    self.selectedBtn.selected = NO;
    [self selectedButton:self.selectedBtn];
    //2.再将当前按钮设置为选中
    button.selected = YES;
    [self selectedButton:button];
    
    //却换视图控制器的事情,应该交给controller来做
    //最好这样写, 先判断该代理方法是否实现
    if (self.selectTabBar) {
        //NSLog(@"%ld,%ld",(long)self.selectedBtn.tag,(long)button.tag);
        self.selectTabBar(self.selectedBtn.tag, button.tag);
    }
    //3.最后把当前按钮赋值为之前选中的按钮
    self.selectedBtn = button;
}

@end
